//
//  AppDelegate.m
//  X-Touch2
//
//  Created by shengchao yang on 12-3-6.
//  Copyright (c) 2012年 foxhis. All rights reserved.
//

#import "AppDelegate.h"
#import "XRRootViewController.h"
#import "XRUpdateViewController.h"
#import "Imports1.h"

#import "MyOrderView.h"
#import "GuestOrderingView.h"
#import "Common.h"
#import "DownloadFromInternet.h"

#import <mach/mach.h>
#import <mach/mach_host.h>

@implementation AppDelegate

@synthesize window;
@synthesize root;
@synthesize update;

#pragma mark - Application lifecycle
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //启动监控内存线程
//    NSThread * thread = [[NSThread alloc] initWithTarget:self selector:@selector(startTest) object:nil];
//    [thread start];//启动更新入口
//    [thread release];
    
#if TARGET_IPHONE_SIMULATOR
    MyNSLog(@"serialNumber=%@", DeviceSerialNumber);
    MyNSLog(@"DBRealPath=%@", DBRealPath);
    MyNSLog(@"DBDemoPath=%@", DBDemoPath);
    MyNSLog(@"XRTranslateRealPath=%@", XRTranslateRealPath);
    
    MyNSLog(@"uniqueDeviceIdentifier=%@",[[UIDevice currentDevice] uniqueDeviceIdentifier ]);
    MyNSLog(@"uniqueGlobalDeviceIdentifier=%@",[[UIDevice currentDevice] uniqueGlobalDeviceIdentifier]);
    MyNSLog(@"str md5=%@",[@"test" stringFromMD5]);
#endif
    
    //数据库的初始化
    if (InitDB) {
        [Common initDBWithDefaultData];
    }else{
        if (![FileDefaultManager fileExistsAtPath:DBRealPath]) {
            [Common initDBWithDefaultDB];
        }
    }
//    [Common createTables];//test
//    [DataBaseAccess testPinyin];//test    
    
    [Common checkSettings];//初始化设置信息
    //初始化默认语言
    NSString *currentLanguage = [[NSLocale preferredLanguages] objectAtIndex:0];
    MyNSLog(@"Current Language=%@", currentLanguage);
    if ([AppSession.supportLangs isContainString:currentLanguage]) {
        AppSession.language = currentLanguage;
    }else {
        AppSession.language = DefaultLang;//对于不支持的语言默认显示中文
    }
    
    //初始化翻译对照文件
    if (![FileDefaultManager fileExistsAtPath:XRTranslateRealPath]) {
        [Common initXRTranslateWithDefault];
    }
    if ([FileDefaultManager fileExistsAtPath:XRTranslateRealPath]) {
        AppSession.translate = [NSDictionary dictionaryWithContentsOfFile:XRTranslateRealPath];
    }else {
        AppSession.translate = [NSDictionary dictionaryWithContentsOfFile:XRTranslateDemoPath];
    }
    
    //判断是下载菜谱界面还是进入推荐菜界面
//    AppSession.isUpdate = YES;//test
//    AppSession.ipadUdid = @"b3b0bf06ccf67e7cebeb30dacd374f18";//test
//    AppSession.isOEMDisplay = YES;//test
    
	if (AppSession.isUpdate) {
		[self.window addSubview:update.view];//进入菜谱下载界面
	}else {
		[self.window addSubview:root.view];//进入推荐菜界面
	}
    [self.window makeKeyAndVisible];
    return YES;
}
//检测内存
-(void)startTest{
    while (true) {
        mach_port_t host_port = mach_host_self();
        mach_msg_type_number_t host_size = HOST_VM_INFO_COUNT;
        vm_size_t pagesize;
        
        host_page_size(host_port, &pagesize);
        vm_statistics_data_t vm_stats;
        
        if (host_statistics(host_port,HOST_VM_INFO,(host_info_t)&vm_stats, &host_size) != KERN_SUCCESS) {
            NSLog(@"Faild to fetch vm statistics");
        }
        
        /*Stats in bytes*/
        natural_t mem_used = (vm_stats.active_count + vm_stats.inactive_count + vm_stats.wire_count) * pagesize;
        natural_t mem_free = vm_stats.free_count * pagesize;
        natural_t mem_total = mem_used + mem_free;
        NSLog(@"used:%fMB  free:%fMB  total:%fMB", mem_used/1024.0/1024.0, mem_free/1024.0/1024.0, mem_total/1024.0/1024.0);
        
        [NSThread sleepForTimeInterval:1];
    }
//    return ((vm_page_size * vmStats.free_count) / 1024.0) / 1024.0;
}
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{ 
}
-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
}
-(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{
}
- (void)applicationWillResignActive:(UIApplication *)application {
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
}
- (void)applicationWillTerminate:(UIApplication *)application {
}

#pragma mark - Memory management
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
                                                    message:@"内存不足！" 
                                                   delegate:nil 
                                          cancelButtonTitle:@"确定"
                                          otherButtonTitles: nil];
    [alert show];
    [alert release];
}
- (void)dealloc {
    [window release];
    [update release];
    [root release];
    [super dealloc];
}
@end

